package com.zdsoft.finance.product.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zdsoft.finance.base.service.impl.BaseServiceImpl;
import com.zdsoft.finance.common.base.CustomRepository;
import com.zdsoft.finance.common.exception.BusinessException;
import com.zdsoft.finance.product.entity.Product;
import com.zdsoft.finance.product.entity.ProductRepayOrder;
import com.zdsoft.finance.product.repository.ProductRepayOrderRepository;
import com.zdsoft.finance.product.service.ProductRepayOrderService;
import com.zdsoft.finance.product.service.ProductService;
import com.zdsoft.finance.spi.product.dto.ProductProjectStatus;
import com.zdsoft.finance.spi.product.dto.ProductRepay;
import com.zdsoft.framework.core.common.domain.BaseEntity;
import com.zdsoft.framework.core.common.util.ObjectHelper;

@Service("productNormalRepayService")
public class ProductRepayOrderServiceImpl extends BaseServiceImpl<ProductRepayOrder, CustomRepository<ProductRepayOrder,String>> implements ProductRepayOrderService{

	@Autowired
	private ProductRepayOrderRepository productRepayOrderRepository;
	@Autowired
	private ProductService productService;
	
	@Override
	@Transactional
	public void initRepayOrder(Product product) throws BusinessException {
		if(ObjectHelper.isEmpty(product) || ObjectHelper.isEmpty(product.getId())){
			throw new BusinessException("传入产品未空");
		}
		product = productService.findOne(product.getId());
		if(ObjectHelper.isEmpty(product)){
			throw new BusinessException("未查询到产品");
		}
		
		ProductRepayOrder productRepayOrder=new ProductRepayOrder();
		//正常项目
		//本金
		productRepayOrder.setIsDeleted(!BaseEntity.DELETED);
		productRepayOrder.setFirstOrder(1);
		productRepayOrder.setStatus(ProductProjectStatus.NORMAL.getValue());
		productRepayOrder.setReceivablesItemCd(ProductRepay.BJ.getReceivablesItemCd());
		productRepayOrder.setReceivablesItemNm(ProductRepay.BJ.getReceivablesItemNm());
		productRepayOrder.setProduct(product);
		productRepayOrderRepository.save(productRepayOrder);
		//复利
		productRepayOrder=new ProductRepayOrder();
		productRepayOrder.setIsDeleted(!BaseEntity.DELETED);
		productRepayOrder.setFirstOrder(2);
		productRepayOrder.setStatus(ProductProjectStatus.NORMAL.getValue());
		productRepayOrder.setReceivablesItemCd(ProductRepay.FL.getReceivablesItemCd());
		productRepayOrder.setReceivablesItemNm(ProductRepay.FL.getReceivablesItemNm());
		productRepayOrder.setProduct(product);
		productRepayOrderRepository.save(productRepayOrder);
		//罚息
		productRepayOrder=new ProductRepayOrder();
		productRepayOrder.setIsDeleted(!BaseEntity.DELETED);
		productRepayOrder.setFirstOrder(3);
		productRepayOrder.setStatus(ProductProjectStatus.NORMAL.getValue());
		productRepayOrder.setReceivablesItemCd(ProductRepay.FX.getReceivablesItemCd());
		productRepayOrder.setReceivablesItemNm(ProductRepay.FX.getReceivablesItemNm());
		productRepayOrder.setProduct(product);
		productRepayOrderRepository.save(productRepayOrder);
		//利息
		productRepayOrder=new ProductRepayOrder();
		productRepayOrder.setIsDeleted(!BaseEntity.DELETED);
		productRepayOrder.setFirstOrder(4);
		productRepayOrder.setStatus(ProductProjectStatus.NORMAL.getValue());
		productRepayOrder.setReceivablesItemCd(ProductRepay.LX.getReceivablesItemCd());
		productRepayOrder.setReceivablesItemNm(ProductRepay.LX.getReceivablesItemNm());
		productRepayOrder.setProduct(product);
		productRepayOrderRepository.save(productRepayOrder);
		
		//不正常项目
		//本金
		productRepayOrder=new ProductRepayOrder();
		productRepayOrder.setIsDeleted(!BaseEntity.DELETED);
		productRepayOrder.setFirstOrder(1);
		productRepayOrder.setStatus(ProductProjectStatus.NOTNORMAL.getValue());
		productRepayOrder.setReceivablesItemCd(ProductRepay.BJ.getReceivablesItemCd());
		productRepayOrder.setReceivablesItemNm(ProductRepay.BJ.getReceivablesItemNm());
		productRepayOrder.setProduct(product);
		productRepayOrderRepository.save(productRepayOrder);
		//复利
		productRepayOrder=new ProductRepayOrder();
		productRepayOrder.setIsDeleted(!BaseEntity.DELETED);
		productRepayOrder.setFirstOrder(2);
		productRepayOrder.setStatus(ProductProjectStatus.NOTNORMAL.getValue());
		productRepayOrder.setReceivablesItemCd(ProductRepay.FL.getReceivablesItemCd());
		productRepayOrder.setReceivablesItemNm(ProductRepay.FL.getReceivablesItemNm());
		productRepayOrder.setProduct(product);
		productRepayOrderRepository.save(productRepayOrder);
		//罚息
		productRepayOrder=new ProductRepayOrder();
		productRepayOrder.setIsDeleted(!BaseEntity.DELETED);
		productRepayOrder.setFirstOrder(3);
		productRepayOrder.setStatus(ProductProjectStatus.NOTNORMAL.getValue());
		productRepayOrder.setReceivablesItemCd(ProductRepay.FX.getReceivablesItemCd());
		productRepayOrder.setReceivablesItemNm(ProductRepay.FX.getReceivablesItemNm());
		productRepayOrder.setProduct(product);
		productRepayOrderRepository.save(productRepayOrder);
		//利息
		productRepayOrder=new ProductRepayOrder();
		productRepayOrder.setIsDeleted(!BaseEntity.DELETED);
		productRepayOrder.setFirstOrder(4);
		productRepayOrder.setStatus(ProductProjectStatus.NOTNORMAL.getValue());
		productRepayOrder.setReceivablesItemCd(ProductRepay.LX.getReceivablesItemCd());
		productRepayOrder.setReceivablesItemNm(ProductRepay.LX.getReceivablesItemNm());
		productRepayOrder.setProduct(product);
		productRepayOrderRepository.save(productRepayOrder);
	}

	@Override
	public List<ProductRepayOrder> queryByCons(ProductRepayOrder productRepayOrder) throws BusinessException {
		return productRepayOrderRepository.queryByCons(productRepayOrder);
	}

	@Override
	@Transactional
	public void updateProductRepayFirstOrder(String[] normal, String[] notNormal) throws BusinessException {
		for(int i=0;i<normal.length;i++){
			ProductRepayOrder productRepayOrder=productRepayOrderRepository.findOne(normal[i]);
			if(ObjectHelper.isEmpty(productRepayOrder)){
				throw new BusinessException("该项还款配置不存在");
			}
			productRepayOrder.setFirstOrder(i+1);
			productRepayOrderRepository.updateEntity(productRepayOrder);
		}
		
		for(int i=0;i<notNormal.length;i++){
			ProductRepayOrder productRepayOrder=productRepayOrderRepository.findOne(notNormal[i]);
			if(ObjectHelper.isEmpty(productRepayOrder)){
				throw new BusinessException("该项还款配置不存在");
			}
			productRepayOrder.setFirstOrder(i+1);
			productRepayOrderRepository.updateEntity(productRepayOrder);
		}
	}

}
